package stu.sup.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.entity.ClassTable;
import stu.sup.entity.ClassTableExample;
import stu.sup.entity.Product;
import stu.sup.mapper.ClassTableMapper;
import stu.sup.mapper.ProductMapper;
import stu.sup.service.IClassTableService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by HUAWEI on 2020/6/23.
 */
@Service
@Transactional
public class ClassTableServiceImpl implements IClassTableService{
    @Autowired
    private ClassTableMapper classTableMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> selectbyBrand(String brand) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andProductBrandEqualTo(brand);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public List<Product> selectbyPlace(String place) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andProductPlaceEqualTo(place);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public List<Product> selectByTime(Date time) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andProductTimeEqualTo(time);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public List<Product> selectByweigt(String weigt) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andProductWeightEqualTo(weigt);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public List<Product> selectByway(String way) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andProductSalewayEqualTo(way);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public Integer add(ClassTable classTable) {
        int count=classTableMapper.insert(classTable);
        return count;
    }



    @Override
    public List<Product> selectbycalssname(String classname) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andClassNameEqualTo(classname);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        List<Product> products=new ArrayList<>();
        for (ClassTable c:classTables){
            Product product=productMapper.selectByPrimaryKey(c.getProductId());
            products.add(product);
        }
        return products;
    }

    @Override
    public ClassTable selectbyid(int id) {
        return classTableMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ClassTable> find(String name) {
        ClassTableExample example=new ClassTableExample();
        ClassTableExample.Criteria criteria=example.createCriteria();
        criteria.andClassNameEqualTo(name);
        List<ClassTable> classTables=classTableMapper.selectByExample(example);
        return classTables;
    }
}
